$('#xingming').focus();
function getPath(xypath) {
	var path = xypath;
	$(function() {
		//select模拟
		$(".dropdown-menu li").click(function(){
			var bText = $(this).text();
			var bValue = $(this).val();
			$(this).parent().siblings(".dropdown-toggle").html(bText+'<b></b>');
			$(this).parent().css("display","none");
		 });
		//控制性别点击样式
		$(".radio-label").on("click",function(){
		  $(this).addClass("on").siblings().removeClass("on");
		});
		
		
		var deleteId = "";//删除关联人的id
		//删除关联人
		$(document).on('click','.pointer2',function(){
			$(this).parent().parent().remove();
			var id = $(this).parent().parent().attr("value");
			deleteId += ","+id;
			var ids = $('#guanlianrids').val();
			$('#guanlianrids').val(ids.replace(','+id,''));
			fenzuglrids = fenzuglrids.replace(','+id,'');
			if(fenzuglrids=="") {
				$("#fenzuid").val(null);
			}
		});
		
		
		
		//返回按钮
		$('.btn-xymb-cancel').click(function(){
			window.location.href=path+"toHuanzhe.html";
		});
		
	  vilidateform();
      function vilidateform() {
	   	  $('form').on('valid.form', function(e){
   			  $("#msg_holder").html("");
   			  $('#hinianlingdw').val($('.dropdown-toggle').text());
   			  var shoujihm = $('#shoujihm').val();
   			  if(shoujihm!='') {
   				var length = 0; 
 				$.ajax({
 		    		url:'./huanzhe/getHuanzheByxmAndsj',
 		    		type:'post',
 		    		data:{shoujihm:shoujihm},
 		    		async:false,
 		    		success:function(data) {
 		    			length = data.length;
 		    		}
 		    	});
 				if(length > 0) {
 					notifyWarning('该手机号码已存在');
 					return false;
 				}else{
 					$('#btnSubmit').attr('disabled',true);
 				}
   			  }
   			  $.ajax({
	    		url:'./huanzhe/xiugaifzid',
	    		type:'post',
	    		data:{deleteId:deleteId},
	    		success:function(data) {
	    			$('#btnSubmit').attr('disabled',true);
	    			$.ajax({
			    		url:'./huanzhe/xinjianhz',
			    		type:'post',
			    		data:$('#formSave').serialize()+"&ybcanbaoryjbxxstr=" + $('#huanzhexx').val(),
			    		success:function(data) {
	    				//是否请求成功判断
	    				if(data=='true'){
	    					var sjc= new Date().getTime();
	    					var presuccmsg=encodeURI(encodeURI('保存成功'));
	    					window.location.href=path+"/views/huanzhe/huanzhe-index.jsp?msg="+presuccmsg+"&sjc="+sjc;
	    				}else{
	    					notifyDanger("保存失败");
	    				}
			    			return false; 
			    		}
			    	});
	    		}
	    	});
	   	  });
     }
	});
}
//根据文本框中的值查询患者
function getHuanzhe(obj) {
	keyCode = event.which;
	if (keyCode == 38|| keyCode == 40|| keyCode == 13) {
		return;
	}
	var val = $(obj).val();
	$(obj).siblings(".f-input-li").css("display","block");
	var str = "";
		$.ajax({
			url:'./huanzhe/getHuanzheByxm',
			type:'post',
			data:{xingming:val},
			async:false, //要将返回数据设置进全局变量，所以请求方式设置为同步请求
			success:function(data) {
				if(data!=null) {
					$(data).each(function(){
						var shoujihm = this.shoujihm;
						if(shoujihm==null) {
							shoujihm = "";
						}
						str+='<li index="'+this.id+','+this.xingming+','+shoujihm+','+this.fenzuid+'">'+this.xingming+'&nbsp;&nbsp;&nbsp;'+shoujihm+'</li>'
					});
				}
			}
		});

	$(obj).parent().find(".dropdown-menu").html("");
	$(obj).parent().find(".dropdown-menu").append(str);
}


$(document).on("mouseover",".f-input-li li",function(){
	$(this).addClass("hello");
});
$(document).on("mouseout",".f-input-li li",function(){
	$(this).removeClass("hello");
});

var fenzuglrids = ""; //存在分組的关联人的id

$(document).on("click",".f-input-li li",function(){
			var bText = $(this).text();
			var bValue = $(this).attr("index");
			var str = bValue.split(",");
			//通过分组id查询该患者是否存在分组
			if($.trim(str[3])!="null") {
				var fenzuid = $("#fenzuid").val();
				if(fenzuid=="") {
					$.ajax({
						url:'./huanzhe/getHuznzheByFenzuId',
						type:'post',
						data:{fenzuid:str[3]},
						success:function(data) {
							var strTr = "";
							if(data!=null) {
								$(data).each(function(){
									var shoujihm = this.shoujihm;
									if(this.shoujihm==null) {
										shoujihm = "";
									}
									strTr+='<tr value="'+this.id+'" class="medicine-item-tr"><td  class="hzn-name"><label class=" mi-table-label2" ></label></td>'+
						               '<td class="hzn-name-td2"><label class=" mi-table-label2" >'+this.xingming+'&nbsp;&nbsp;&nbsp;'+shoujihm+'</label><span class="shanchu pointer2"></span>'+
										'<ul id="dropdownli" class="dropdown-menu li-width f-input-li"  style="display:none;" role="menu"></ul>'+
										'</td>'+
						                '<td class="hzn-name"><label class="mi-table-label2" ></label></td>'+
						                '<td class="hzn-name-td4">'+
						                '</td>'+
					                '</tr>';
						            var ids = $('#guanlianrids').val();
									$('#guanlianrids').val(ids+","+this.id);
									fenzuglrids += ","+this.id;
									
								});
								$("#tbguanlr").append(strTr);
								$("#fenzuid").val($.trim(str[3]));
							}
							
						}
					});
					
				}else{
					var ids = $('#guanlianrids').val();
					if(ids.indexOf(str[0])>0) {//重复添加关联人或者添加自己
						//这里提示不能重复添加患者
						notifyWarning('不能重复添加关联人！');
						$(".f-input-li").css("display","none");
						$('#hz_serch').val("");
						return false;
					}
					if(str[0]==id) {
						notifyWarning('不能添加自己为关联人！');
						$(".f-input-li").css("display","none");
						$('#hz_serch').val("");
						return false;
					}
					//这里提示不能重复添加分组
					//表单提示--warning
					notifyWarning('不能重复添加分组');
				}
				
			}else{
				var ids = $('#guanlianrids').val();
				if(ids.indexOf(str[0])>0) {
					if(str[0]==id) {
						notifyWarning('不能添加自己为关联人！');
						$(".f-input-li").css("display","none");
						$('#hz_serch').val("");
						return false;
					}
					//这里提示不能重复添加患者
					//表单提示--warning
					notifyWarning('不能重复添加联系人');
				}else {
					var clone=
						'<tr value="'+str[0]+'"  class="medicine-item-tr"><td  class="hzn-name"><label class=" mi-table-label2" ></label></td>'+
			               '<td class="hzn-name-td2"><label class=" mi-table-label2" >'+str[1]+'&nbsp;&nbsp;&nbsp;'+str[2]+'</label><span class="shanchu pointer2"></span>'+
							'<ul id="dropdownli" class="dropdown-menu li-width f-input-li"  style="display:none;" role="menu"></ul>'+
							'</td>'+
			                '<td class="hzn-name"><label class="mi-table-label2" ></label></td>'+
			                '<td class="hzn-name-td4">'+
			                '</td>'+
		             '</tr>';
						$("#tbguanlr:last").append(clone);
						
						$('#guanlianrids').val(ids+","+str[0]);
						//$("#fenzuid").val(null);
				}
				
			}
			
			$(".f-input-li").css("display","none");
			$(this).parent().html("");
			$('#hz_serch').val("");
		}); 

$(document).on("focusout",".dropdowninput",function(){
	if($(".f-input-li li").hasClass("hello")){
	$(this).siblings(".f-input-li").css("display","block");
	}else{
	$(this).siblings(".f-input-li").css("display","none");
	}
});

$(document).on("mouseover",".f-input-li2 li",function(){
	$(this).addClass("hello");
})
$(document).on("mouseout",".f-input-li2 li",function(){
	$(this).removeClass("hello");
})
//整体去掉input的autocomplete
$("input:text").attr("autocomplete","off");
//回车转Tab
//检查控件是否可见
function checkobjvisual(e) {


    var objvis = false;
    if (e) {
        if (e.offsetHeight > 0) {
            objvis = true;
        }
        if (e.offsetTop > 0) {
            objvis = true;
        }
        
         if (e.readOnly == true) {
             objvis = false;  
       }  
    }
    return objvis;
}

//查找下一个控件
function focunext(next_id) {


    var code;
    if (!e) {
        var e = window.event;
    }
    if (e.keyCode) {
        code = e.keyCode;
    }
    else if (e.which) {
        code = e.which;
    }
    if (code == 13) {


        if (next_id)
        {document.getElementById(next_id).focus(); }
        else {
            var inputList = document.getElementsByTagName("input");
            // 循坏这个集合，包括了所有的input。
            var nextindex;
            nextindex = -1;
            for (i = 0; i < inputList.length; i++) {
                if (inputList[i] == document.activeElement) {
                    var acobj = document.activeElement;
                    nextindex = i + 1;
                    
                    while ((!checkobjvisual(inputList[nextindex])) && (nextindex <= inputList.length)) {
                        nextindex = nextindex + 1;
                    }
                    if (inputList[nextindex])  inputList[nextindex].focus();
                    break;
                }
            }
            
        }
    }
};
document.onkeydown = function(e) {
	    var code;
	    if (!e) {
	        var e = window.event;
	    }
	    if (e.keyCode) {
	        code = e.keyCode;
	    }
	    else if (e.which) {
	        code = e.which;
	    }
	    if (code == 8) {
	        if (document.activeElement.readOnly == true || document.activeElement.disabled == true)
	        { return false; }
	    }
	    if (code == 13) {
	        if (document.activeElement.type == "textarea") {
	            return;
	        }
	        if (document.activeElement.type != "submit" && document.activeElement.type != "button") {
	 
	             event.keyCode = 9;
	             if(!enterToTab){
			    		enterToTab = true;
			    }else{
			    	focunext();
			    }
	            return false;
	        }
	    }
	   
}

//假定已有证历本号
function huanzhexq(huanzhexq1){
	if(!!huanzhexq1){
		var xiangqing = huanzhexq1.split("|");
		var xingming = xiangqing[4];
		var xinbie = xiangqing[5];
		var shenfenzh = xiangqing[3];
		$('#xingming').val(xingming);
		if(xinbie === '1'){
			$('#nan').attr('checked','true');
			$('#work-label').addClass('on');
			$('#stop-label').removeClass('on');
		}else{
			$('#nv').attr('checked','true');
			$('#work-label').removeClass('on');
			$('#stop-label').addClass('on');
		}
		$('#shenfenzhm').val(shenfenzh);
	}
}
function getYiBaojbrc(chikalx, count) {
	$.ajax({
	    type: "post",
	    url: path + "/yibao/getYbRenyuancbjbxx",
	    async: false,
	    data: {"chikalx": chikalx},
	    success: function(data) {
	    	var result = JSON.parse(data);
	    	if (result.chenggongbz == MSG_FALI) {
	    		getDangqzedzcs();
	    		$('#yb-chongxinqd').val(data);
	    		$('#yb-chongxinqd').trigger('click');
	    		count++;
	    		if (count > 1) {
	    			notifyWarning('服务器忙碌,请稍后再试');
	    			return false;
	    		} 
	    		getYiBaojbrc(chikalx,count);
	    	} else {
	    		$('#yb-duka').val(data);
	    		$('#yb-duka').trigger('click');
	    	}
	    }
	});
}
//医保的读卡按钮
$('#shuaka').click(function (){
	var count = 0;
	getYiBaojbrc(YBDUKALX_SHIMINKA, count);
	var huanzhexxstr = $('#huanzhexx').val();
	if (undefined != huanzhexxstr) {
		huanzhexq(huanzhexxstr);
	}
});